# encoding=utf8
# 网络功能文件
import json
import time
import requests



class ZTCSpider():  # 爬虫类（类名可改）
    def __init__(self,cookies):
        self.now_time = time.strftime('%Y-%m-%d', time.localtime(time.time()))
        self.headers = {
            "cookie":cookies,
            "referer": "https://subway.simba.taobao.com/",
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"
        }

    def get_username(self):
        url_user = "https://subway.simba.taobao.com/bpenv/getLoginUserInfo.htm"
        data_user = {"_referer": "/visitor/info"}
        res_user = requests.post(url_user, data=data_user, headers=self.headers)
        WW_nickName = json.loads(res_user.text)["result"]["nickName"]
        return WW_nickName

    def get_plan_info_dic(self):

        url_plan = "https://subway.simba.taobao.com/openapi/param2/1/gateway.subway/common/campaign/list$"
        data_plan = {
            "sla": "json",
            "isAjaxRequest": "true",
            "token": "856a8bb3",
            "_referer": "/manage/campaign/index?selectedCampaignType=0"
        }
        res_plan = requests.post(url_plan, data=data_plan, headers=self.headers)
        plan_info = json.loads(res_plan.text)["result"]
        plan_info_dic = {}
        for plan in plan_info:
            type = plan['type']  # 计划类型（标准计划：'0',智能计划：'8'）
            if type == '0':
                plan_title = plan['title']
                id = plan['id']
                plan_info_dic[plan_title] = id
        print(plan_info_dic)
        return plan_info_dic


